import formatDate from './formatDate.js'
import { request }  from './util.js'
//填写日期
setInterval(function() {
  document.getElementById('time').innerHTML = formatDate()
}, 1000)

//获得本地存储数据
let userStr = sessionStorage.getItem('user')
let user = JSON.parse(userStr)
let username = user.username
let token = user.token
document.getElementById('title').innerHTML = username

//发起请求拦截
axios.interceptors.request.use(function(request){
    request.headers['Authorization'] = token
    return request
})

//请求商品列表
;(async(pagenum,pagesize) => {
    //1.准备参数
    let url = 'http://192.168.102.7:8888/api/private/v1/goods'
    let params = {
        pagenum,
        pagesize,
        method:'GET'
    }
    //2.执行请求
    let{data:res}= await request(url,params)
    //3.解析结果
    let {goods:goodslist} = res.data
    //4.数据渲染
    appendGoods(goodslist)
})(1,20)

//拼接页面
function appendGoods(goodsList) {
    let app = $('#app')
    let ul = $('<ul></ul>')
    for (let i = 0, len = goodsList.length; i < len; i++) {
      let li = `
            <li><span>${goodsList[i].goods_id}</span>----<span>${goodsList[i].goods_name}</span>----<span>${'$' + goodsList[i].goods_price}</span></li>
          `
      ul.append(li)
    }
    app.append(ul)
  }